﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnAn_critterBehavior : MonoBehaviour {
    int state = 1; //0 begin, 1 move, 2 idle, 3 die, 4 idle2
    bool stateChangeDone = false;
    float timeCounter = 0;
    float behavChangeTime = 1;
    //RANDOM ROT OVER TIME VARIABLES
    bool right = true;
    float timeOfChange;
    float rotTimeCounter;
    float lendulet;
    float rnd;
    float xStart;
    float zStart;
    public float moveSpeed = 0.65f;
    public float moveAnimSpeed = 1;
    public float rotSpeed = 1;
    public float rotTreshold = 80;
    public float killTime = 30;
    public bool move2 = false;
    bool useMove2 = false;
    float killTimeCounter = 0;
    void Start() {
        behavChangeTime = Random.Range(1, 4);
        timeOfChange = Random.Range(0, 3);
        xStart = transform.position.x;
        zStart = transform.position.z;
        GetComponent<Animation>()["move"].speed = moveAnimSpeed;
    }
    void Update() {
        timeCounter += Time.deltaTime;
        killTimeCounter += Time.deltaTime;
        if (timeCounter > behavChangeTime) {
            timeCounter = 0;
            if (state == 1 && stateChangeDone == false) {
                rnd = Random.Range(0, 100);
                if (rnd < 10) state = 2;
                if (rnd > 10 && rnd < 20) state = 4;
                if (rnd < 60) {
                    useMove2 = false;
                    behavChangeTime = Random.Range(1, 4);
                } else {
                    useMove2 = true;
                    behavChangeTime = 0.5f;
                }
                stateChangeDone = true;
            }
            if (state == 2 && stateChangeDone == false) {
                rnd = Random.Range(0, 100);
                if (rnd < 50) state = 1;
                if (rnd > 75) state = 4;
                stateChangeDone = true;
            }
            if (state == 4 && stateChangeDone == false) {
                rnd = Random.Range(0, 100);
                if (rnd < 50) state = 1;
                if (rnd > 75) state = 2;
                stateChangeDone = true;
            }
            stateChangeDone = false;
        }
        if (killTime < killTimeCounter && state != 3) {
            Destroy(gameObject, 4);
            state = 3;
        }
        if (state == 1) { ///MOVE BEGIN
            //********************* RANDOM ROTATE OVER TIME PART
            rotTimeCounter += Time.deltaTime;
            if (rotTimeCounter > timeOfChange) {
                right = !right;
                timeOfChange = Random.Range(0, 3);
                rotTimeCounter = 0;
            }
            if (right == true) {
                lendulet -= 10 * Time.deltaTime * rotSpeed;
            }
            if (right == false) {
                lendulet += 10 * Time.deltaTime * rotSpeed;
            }
            if (lendulet > rotTreshold) {
                lendulet = rotTreshold;
                right = true;
            }
            if (lendulet < -rotTreshold) {
                lendulet = -rotTreshold;
                right = false;
            }
            transform.Rotate(0, lendulet * Time.deltaTime * rotSpeed, 0);
            // MOVE PART
            transform.Translate(new Vector3(0, 0, moveSpeed) * Time.deltaTime);
            // ANIM PART
            if (!GetComponent<Animation>().IsPlaying("move") && (move2 == false)) {
                GetComponent<Animation>().CrossFade("move", 0.3f);
                GetComponent<Animation>()["move"].speed = moveAnimSpeed;
            }
            if (move2 == true) {
                if (!GetComponent<Animation>().IsPlaying("move") && useMove2 == false) {
                    GetComponent<Animation>().CrossFade("move", 0.3f);
                    GetComponent<Animation>()["move"].speed = moveAnimSpeed;
                }
                if (!GetComponent<Animation>().IsPlaying("move2") && useMove2 == true) {
                    GetComponent<Animation>().CrossFade("move2", 0.3f);
                    GetComponent<Animation>()["move2"].speed = moveAnimSpeed;
                }
            }
        }
        if (state == 2) {
            if (!GetComponent<Animation>().IsPlaying("idle")) GetComponent<Animation>().CrossFade("idle", 0.3f);
        }
        if (state == 3) {
            if (!GetComponent<Animation>().IsPlaying("die")) GetComponent<Animation>().CrossFade("die");
        }
        if (state == 4) {
            if (!GetComponent<Animation>().IsPlaying("idle2")) GetComponent<Animation>().CrossFade("idle2", 0.5f);
        }
    }
}
